//+------------------------------------------------------------------+ //| MACD - RSI.mq4 | //| Copyright © 2014, MetaQuotes Software Corp. | //| http://www.metaquotes.net/ | //| made in Russia by lukas1 | //+------------------------------------------------------------------+ #property copyright "Copyright © 2014, MetaQuotes Software Corp." #property link "lukas1@ngs.ru" //---- indicator settings #property indicator_separate_window #property indicator_buffers 3 #property indicator_color1 Silver #property indicator_color2 Red #property indicator_color3 DodgerBlue #property indicator_width1 2 //---- indicator parameters extern int FastEMA=12; extern int SlowEMA=26; extern int SignalSMA=9; //---- input parameters extern int RSIPeriod=14; extern double caliber=4.0; //need for RSI caliber extern bool ShowArrowsForMacdCross = true; extern bool ShowArrowsForRsiCross = true; extern bool ShowArrowsForMacdRsiCross = true; extern string arrowsIdentifier = "macdRsiArrows1"; extern color arrowsUpColor1 = DeepSkyBlue; extern color arrowsDnColor1 = Red; extern color arrowsUpColor2 = PaleVioletRed; extern color arrowsDnColor2 = Gold; //---- buffers double RSIBuffer[]; double PosBuffer[]; double NegBuffer[]; //---- indicator buffers double MacdBuffer[]; double SignalBuffer[]; double trend1[]; double trend2[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { IndicatorBuffers(7); //---- drawing settings SetIndexStyle(0,DRAW_HISTOGRAM); SetIndexStyle(1,DRAW_LINE); SetIndexStyle(2,DRAW_LINE); SetIndexDrawBegin(1,SignalSMA); IndicatorDigits(Digits+1); //---- indicator buffers mapping SetIndexBuffer(0,MacdBuffer); SetIndexBuffer(1,SignalBuffer); SetIndexBuffer(2,RSIBuffer); SetIndexBuffer(3,PosBuffer); SetIndexBuffer(4,NegBuffer); SetIndexBuffer(5,trend1); SetIndexBuffer(6,trend2); //---- name for DataWindow and indicator subwindow label IndicatorShortName("MACD-RSI("+FastEMA+","+SlowEMA+","+SignalSMA+"),("+RSIPeriod+")"); SetIndexLabel(0,"MACD"); SetIndexLabel(1,"Signal"); SetIndexLabel(2,"RSI"); //---- initialization done return(0); } int deinit() { deleteArrows(); return(0); } //+------------------------------------------------------------------+ //| Moving Averages Convergence/Divergence | //+------------------------------------------------------------------+ int start() { int limit; int counted_bars=IndicatorCounted(); double rel,negative,positive; //---- last counted bar will be recounted if(counted_bars>0) counted_bars--; limit=Bars-counted_bars; //---- macd counted in the 1-st buffer for(int i=0; i=RSIPeriod) i=Bars-counted_bars-1; while(i>=0) { double sumn=0.0,sump=0.0; if(i==Bars-RSIPeriod-1) { int k=Bars-2; //---- initial accumulation while(k>=i) { rel=MacdBuffer[k]-MacdBuffer[k+1]; if(rel>0) sump+=rel; else sumn-=rel; k--; } positive=sump/RSIPeriod; negative=sumn/RSIPeriod; } else { //---- smoothed moving average rel=MacdBuffer[i]-MacdBuffer[i+1]; if(rel>0) sump=rel; else sumn=-rel; positive=(PosBuffer[i+1]*(RSIPeriod-1)+sump)/RSIPeriod; negative=(NegBuffer[i+1]*(RSIPeriod-1)+sumn)/RSIPeriod; } PosBuffer[i]=positive; NegBuffer[i]=negative; if(negative==0.0) RSIBuffer[i]=0.0; else RSIBuffer[i]=caliber*Point*(50.0-100.0/(1+positive/negative) ); i--; } for(i=limit; i>=0; i--) { trend1[i] = trend1[i+1]; trend2[i] = trend2[i+1]; if (SignalBuffer[i]MacdBuffer[i]) trend1[i] = -1; if (SignalBuffer[i]RSIBuffer[i]) trend2[i] = -1; manageArrow(i); } return(0); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // void manageArrow(int i) { if (ShowArrowsForMacdCross || ShowArrowsForRsiCross) { if (ShowArrowsForMacdCross) { deleteArrow("1",Time[i]); if (trend1[i]!=trend1[i+1]) { if (trend1[i] == 1) drawArrow(i,"1",arrowsUpColor1,241,false); if (trend1[i] ==-1) drawArrow(i,"1",arrowsDnColor1,242,true); } } if (ShowArrowsForRsiCross) { deleteArrow("2",Time[i]); if (trend2[i]!=trend2[i+1]) { if (trend2[i] == 1) drawArrow(i,"2",arrowsUpColor2,241,false); if (trend2[i] ==-1) drawArrow(i,"2",arrowsDnColor2,242,true); } } } } // // // // // void drawArrow(int i,string add, color theColor,int theCode,bool up) { string name = arrowsIdentifier+":"+add+":"+Time[i]; double gap = 3.0*iATR(NULL,0,20,i)/4.0; // // // // // ObjectCreate(name,OBJ_ARROW,0,Time[i],0); ObjectSet(name,OBJPROP_ARROWCODE,theCode); ObjectSet(name,OBJPROP_COLOR,theColor); if (up) ObjectSet(name,OBJPROP_PRICE1,High[i]+gap); else ObjectSet(name,OBJPROP_PRICE1,Low[i] -gap); } // // // // // void deleteArrows() { string lookFor = arrowsIdentifier+":"; int lookForLength = StringLen(lookFor); for (int i=ObjectsTotal()-1; i>=0; i--) { string objectName = ObjectName(i); if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName); } } void deleteArrow(string add, datetime time) { string lookFor = arrowsIdentifier+":"+add+":"+time; ObjectDelete(lookFor); }